package com.itboye.mengwu.data.repository;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.text.TextUtils;

import com.google.gson.reflect.TypeToken;
import com.itboye.core.interfaces.ICompleteListener;
import com.itboye.core.utils.BaseErrorListener;
import com.itboye.core.utils.BaseSuccessReqListener;
import com.itboye.core.utils.ByJsonRequest.Builder;
import com.itboye.mengwu.data.interfaces.IOrder;
import com.itboye.mengwu.data.model.DapeigouBean;
import com.itboye.mengwu.data.model.OrderBean;
import com.itboye.mengwu.data.model.OrderDetailBean;
import com.itboye.mengwu.data.model.OrderQueryModel;
import com.itboye.mengwu.data.model.StoreFavourableBean;
import com.itboye.mengwu.ui.shop.DaPaiGouRootBean;

public class OrderRepository extends BaseNetRepository implements
		IOrder<OrderBean> {
	public static final String OrderAdd = "BY_Orders_addNew";// 8.2 生成订单
	public static final String GoodsAddCollection = "BY_Product_favorites";// 5.4
																			// 收藏
																			// 和
																			// 取消
	public static final String CollectionGoodsQuery = "BY_Product_getFavorites";// 5.5
																				// 收藏商品分页（分页查询）
	public static final String OrderQuery = "BY_Orders_query";// 订单查询
	public static final String OrderDetail = "BY_Orders_detail";// 订单详情
	public static final String OrderDelete = "BY_Orders_cancel";// 删除订单
	public static final String OrderQueren="BY_Orders_sure";//确认收货
	public static final String ShenQingShouHou="BY_Orders_aftersale";//申请售后
	public static final String BuyInstant="BY_Orders_addBySkuNew";//立即购买
	public static final String CommentOrder="BY_Orders_comment";//评论订单
	public static final String StoreFavourable="BY_StorePromotion_getInfo";//获取店铺优惠
	public static final String dapeigou="BY_Product_packageDetail";//搭配购详情
	String api = "100";

	public OrderRepository(ICompleteListener listener) {
		// TODO Auto-generated constructor stub
		super(listener);
	}

	@Override
	public void add(OrderBean model) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<OrderBean>() {
		}.getType();
		String apiVer = "100";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("uid", model.getUid());
		map.put("cart_ids", model.getCartIds());
		map.put("address_id", model.getAddressId());
		map.put("notes",model.getNotes());
		map.put("red_ids", model.getRedIds());
//		if(model.getMoney()!=null)
//		map.put("money", model.getMoney());
		map.put("coupon_ids", model.getCouponIds());
		(new Builder<OrderBean>())
				.setTypeVerParamsAndReturnClass(OrderAdd, apiVer, map, type)
				.requestListener(
						new BaseSuccessReqListener<OrderBean>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void delete(OrderBean model) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<String>() {
		}.getType();
		String apiVer = api;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("uid", model.getUid());
		map.put("order_code", model.getCode());
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(OrderDelete, apiVer, map, type)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void update(OrderBean model) {
		// TODO Auto-generated method stub

	}

	@Override
	public void getInfo(OrderBean model) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<OrderQueryModel>() {
		}.getType();
		String apiVer = api;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("uid", model.getUid());
		map.put("status", model.getStatus());
		map.put("page_no", model.getPageNo());
		(new Builder<OrderQueryModel>())
				.setTypeVerParamsAndReturnClass(OrderQuery, apiVer, map, type)
				.requestListener(
						new BaseSuccessReqListener<OrderQueryModel>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public List<OrderBean> query(OrderBean model) {

		return null;
	}

	@Override
	public void getXiangQing(String id) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<OrderDetailBean>() {
		}.getType();
		String apiVer = api;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("oid", id);
		(new Builder<OrderDetailBean>())
				.setTypeVerParamsAndReturnClass(OrderDetail, apiVer, map, type)
				.requestListener(
						new BaseSuccessReqListener<OrderDetailBean>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void makesureOrder(String uid, String orderCode) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<String>() {
		}.getType();
		String apiVer = api;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("uid", uid);
		map.put("order_code", orderCode);
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(OrderQueren, apiVer, map, type)
				.requestListener(
						new BaseSuccessReqListener<String>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void shenQingShouHou(String uid, String orderCode, String money,
			String reason, String backtype,boolean isGoodsGet,String note) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<String>() {
		}.getType();
		String apiVer = "100";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("uid", uid);
		map.put("order_code", orderCode);
		map.put("reason", reason);
		map.put("refund_money", money);
		map.put("refund_type", backtype);
		map.put("goods_get", isGoodsGet==true?0:1);
		map.put("note", note);
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(ShenQingShouHou, apiVer, map, type)
				.requestListener(
						new BaseSuccessReqListener<String>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void buyInstant(String uid, String pid, String count, String pskuId,
			String redId, String couponId, String money, String note,
			String addressId, String form,String packageId) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<OrderBean>() {
		}.getType();
		String apiVer = api;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", uid);
		params.put("pid", pid);
		params.put("count", count);
		params.put("psku_id", pskuId);
		if(!"".equals(couponId))
		params.put("coupon_id", couponId);
//		if(money!=null)
//		params.put("money", money);
		params.put("address_id", addressId);
		if(!"".equals(redId))
		params.put("red_id", redId);
		params.put("from", "2");
		if(TextUtils.isEmpty(note))
		params.put("note",note);
		if(!TextUtils.isEmpty(packageId))
			params.put("package_id", packageId);
		// params.put("total_price", totalPrice);
		// params.put("desc", desc);
		(new Builder<OrderBean>())
				.setTypeVerParamsAndReturnClass(BuyInstant, apiVer, params, type)
				.requestListener(
						new BaseSuccessReqListener<OrderBean>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void commentOrder(String uid, String pid, String orderCode,String pskuId,String groupId,String packageId,
			String score, String[] comment, String imgs, String expressScore,
			String deliveryScore, String serviceScore) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<String>() {
		}.getType();
		String apiVer = "101";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", uid);
		params.put("pid", pid);
		params.put("order_code", orderCode);
		params.put("psku_id", pskuId);
		params.put("score", score);
		
		params.put("comment", comment);
		params.put("attachments", imgs==null?"":imgs);
		params.put("package_id", packageId);
		params.put("group_id", groupId);
//		params.put("express_score", expressScore);	
//		params.put("delivery_score", deliveryScore);
//		params.put("service_score", serviceScore);
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(CommentOrder, apiVer, params, type)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void getStoreFavourable(String storeId) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<StoreFavourableBean>() {
		}.getType();
		String apiVer = "100";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("store_id", storeId);
		(new Builder<StoreFavourableBean>())
				.setTypeVerParamsAndReturnClass(StoreFavourable, apiVer, params, type)
				.requestListener(
						new BaseSuccessReqListener<StoreFavourableBean>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
		
	}

	@Override
	public void dapeigou(String packageId) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<DaPaiGouRootBean>() {
		}.getType();
		String apiVer = "100";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("package_id", packageId);
		(new Builder<DaPaiGouRootBean>())
				.setTypeVerParamsAndReturnClass(dapeigou, apiVer, params, type)
				.requestListener(
						new BaseSuccessReqListener<DaPaiGouRootBean>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
		
	}

}
